468A - 24 Game - CodeForces Solution


constructive algorithms greedy math *1500

Please click on ads to support us..

Python Code:

import sys
import threading
from random import randint
import os
from io import BytesIO, IOBase

from collections import defaultdict, Counter, deque
from math import inf, sqrt, floor
from functools import lru_cache
from heapq import heappop, heappush, heapify
import math
from bisect import bisect_left, bisect_right
 
nextInts = lambda: list(map(int, input().split()))
nextInt = lambda: int(input())
Mod = 10**9 + 7

def remove(n):
    print(str(n), "-", str(n-1), "=", "1")
    print(1, "*", 1, "=", 1)

def solution():
    n = nextInt()
    if n < 4:
        print("NO")
        return

    print("YES")
    for i in range(5 + n%2, n,2)[::-1]:
        remove(i+1)

    if n % 2:
        print("2 - 1 = 1")
        print("4 * 5 = 20")
        print("1 + 3 = 4")
        print("4 + 20 = 24")
    else:
        print("1 * 2 = 2")
        print("2 * 3 = 6")
        print("6 * 4 = 24")

def main():

    t = 1
        for _ in range(t):
        solution()



BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")


sys.setrecursionlimit(10**6)
threading.stack_size(1 << 27)
thread = threading.Thread(target=main)
thread.start(); thread.join()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
template<class T>inline T Min(register T x,register T y){
	return x<y?x:y;
}
template<class T>inline T Max(register T x,register T y){
	return x>y?x:y;
}
template<class T>inline void Cmin(register T &x,register T y){
	x=Min(x,y);
}
template<class T>inline void Cmax(register T &x,register T y){
	x=Max(x,y);
}
inline long long read(){
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9'){
        if(ch=='-'){
            f=-1;
        }
        ch=getchar();
    }
    while(ch>='0' && ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=getchar();
    }
    return x*f;
}
inline bool write(long long x,char ch='\n'){
    static char buf[64];
    static long long len=-1;
    if(x<0){
        putchar('-');
        x=-x;
    }
    do{
        buf[++len]=x%10;
        x/=10;
    }
    while(x);
    while(len>=0){
        putchar(buf[len--]+'0');
    }
    putchar(ch);
    return true;
}
long long n;
int main(){
	n=read();
    if(n<=3){
        return !printf("NO\n");
    }
    else if(n>=4){
    	printf("YES\n");
	    if(n%2==1){
	    	printf("5 * 4 = 20\n20 + 3 = 23\n23 + 2 = 25\n25 - 1 = 24\n");
		    for(register int i=n;i>=6;i-=2){
		    	printf("%d - %d = 1\n24 * 1 = 24\n",i,i-1);
		    }
	    }
	    else{
	    	printf("1 + 2 = 3\n3 + 3 = 6\n4 * 6 = 24\n");
		    for(register int i=n;i>=5;i-=2){
		    	printf("%d - %d = 1\n24 * 1 = 24\n",i,i-1);
		    }
		}
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff